﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Http.Cors;

namespace Disco.Web.Api
{
    public static class CorsConfig
    {
        public static void Register(HttpConfiguration config)
        {
            var allowedOrigins = ConfigurationManager.AppSettings["Access-Control-Allow-Origin"] ?? "http://localhost";
            var allowedHeaders = ConfigurationManager.AppSettings["Access-Control-Allow-Headers"] ?? "authorization,content-type,dataserviceversion,maxdataserviceversion,accept";
            var allowedMethods = ConfigurationManager.AppSettings["Access-Control-Allow-Methods"] ?? "GET,POST,PUT,DELETE,PATCH,OPTIONS";
            var exposedHeaders = ConfigurationManager.AppSettings["Access-Control-Expose-Headers"] ?? "dataserviceversion,maxdataserviceversion";

            var options = new EnableCorsAttribute(allowedOrigins, allowedHeaders, allowedMethods, exposedHeaders);
            config.EnableCors(options);
        }
    }
}